<?php
/**
 * @author 	 	 supfire@sohu.com
 * @copyright		 yctool.com
 * @version		 2.2
 * @name 		 user.php
 * @todo 		 用户页
 * @final 		 2009-09-05
 */
define('IN_YCTOOL', true);
require_once('config.php');
require_once(DIR_COMM . 'common.php');

$userid = $user->userinfo['userid'];
$username = $user->userinfo['username'];
$userpath = 'userdata/'.func::getFilePath($userid, 'config');


if(file_exists($userpath . 'face.jpg'))
{
    $pimg = $userpath . 'face.jpg';
}
else
{
    $pimg = 'common/img/uploadshow.gif';
}

if (!$user->userinfo['userid'] || !$userpath)
{
    func::obHeader('./login.php');
    exit();
}   
!$theurl && $theurl = 'cutpic.php';
//配置最终图片的大小
$facewidth = 180;
$faceheight = 180;
//配置小图尺寸
$picwidth = 56;
$picheight = 56;

    $userface = $userpath.'face';
    $cutpicPost = $_POST['cutpicPost'];

    if(!file_exists($userface . '0.jpg'))
    {
	$userface0 = '/javascript/cutpic/image/man.gif';
    }
    else
    {
	$userface0 = $userface . '0.jpg';
    }

    switch ($cutpicPost)
    {
    case 'cuppicup':
	$img = $_FILES["face"];
	if($img["size"])
	{
	    func::errorUploadFIle($img, 2000);
	    func::imageResize($img["tmp_name"], 1600, 1600, $userface.'tmp.jpg');
	}
	else
	{
	    func::alertMsg('上传的图片不能为空'); 
	}
	unset($img);
	break;
    case 'facesave':
	$jpeg_quality = 100;
 	$src = $_POST['bigImage'];
	if(file_exists($src))
	{
	    $src_im = imagecreatefromjpeg($src);
	    $intImage_Width = imagesx($src_im); 
	    $intImage_Height = imagesy($src_im); 

	    $dst_w = $facewidth;
	    $dst_h = $faceheight;

	    $dst_im = imagecreatetruecolor($dst_w, $dst_h);

	    // 填充背景色
	    $white = imagecolorallocate($dst_im, 255, 255, 255);
	    imagefill ($dst_im, 0,0, $white);

	    $dst_x = 0;
	    $dst_y = 0;
	    $src_x = $_POST['txt_left']/$_POST['txt_Zoom'];
	    $src_y = $_POST['txt_top']/$_POST['txt_Zoom'];

	    $src_w = $dst_w/$_POST['txt_Zoom'];
	    $src_h = $dst_h/$_POST['txt_Zoom'];

	    if (function_exists('imagecopyresampled'))
	    {
		imagecopyresampled($dst_im,$src_im,$dst_x,$dst_y,$src_x,$src_y,$dst_w,$dst_h,$src_w,$src_h);
	    }
	    else
	    {
		imagecopyresized($dst_im,$src_im,$dst_x,$dst_y,$src_x,$src_y,$dst_w,$dst_h,$src_w, $src_h);
	    }
            $white = imagecolorallocate($dst_im, 255, 255, 255);
            //填充背景，以免截取超出图片时成黑色
            $intImage_Width_Zoom = $intImage_Width * $_POST['txt_Zoom'];
            $intImage_Height_Zoom = $intImage_Height * $_POST['txt_Zoom'];
            $src_x_Zoom = $_POST['txt_left'];
            $src_y_Zoom = $_POST['txt_top'];
            /*
            echo '<hr>';
            echo $intImage_Width_Zoom;//图片缩放后宽度
            echo '<hr>';
            echo $src_x_Zoom;//缩放后相对于上顶角的偏移宽度
            echo '<hr>';
            echo $facewidth;//最终截取宽度
            echo '<hr>';
            //*/
            if( ($intImage_Width_Zoom - $src_x_Zoom) < $facewidth || ($intImage_Height_Zoom - $src_y_Zoom) < $faceheight )
            {
                imagefill($dst_im, ($facewidth - 1), ($faceheight - 1), $white);   
            }
            if($src_x < 0 || $src_y < 0)
            {
                imagefill($dst_im, 0, 0, $white);            
            }

	    imagejpeg($dst_im, $userface.'0.jpg', 100);
            func::imageResize($userface.'0.jpg', $picwidth, $picheight, $userface.'.jpg', 1);
            @copy($userface.'.jpg', $userface.'tmp.jpg');
            //@unlink($userface.'tmp.jpg');
	    $user->facetrue($userid, $userface.'.jpg');
	    msg::insertAction("($userid, 0, 3, '<div class=l50_s>
		    <a href=\"user.php?uid=".$userid."\"><IMG src=".$userface.".jpg height=".$picheight." width=".$picwidth." border=0></a>
                    </div>', ".time().", '')");
            echo '<script language="javascript">window.parent.document.getElementById("faceok").value = "'.$userface.'";</script>';
	    echo ('<div class="facebox">
    		<img src="'.$userface.'0.jpg" id="ImageDrag1" style="float:center;BORDER: #cccccc 1px solid; PADDING:6px">
                </div> 
                <script language="javascript"> document.getElementById("ImageDrag1").src="'.$userface.'0.jpg"+"?temp="+ Date.parse(new Date()); </script>
                '); 
            exit;
	}
	break;
    case 'upfile':
	$img = $_FILES["face2"];
	if($img["size"])
	{
	    func::errorUploadFIle($img,2000);
	    func::imageResize($img["tmp_name"], $facewidth, $faceheight, $userface.'0.jpg', 1);
	    func::imageResize($img["tmp_name"], $picwidth, $picheight, $userface.'.jpg', 1);
	    $user->facetrue($userid, $userface.'.jpg');
	    msg::insertAction("($userid, 0, 3, '<div class=l50_s>
		<a href=\"user.php?uid=".$userid."\"><IMG src=".$userface.".jpg height=".$picheight." width=".$picwidth." border=0></a>
                </div>', ".time().", '')");
            echo '<script language="javascript">window.parent.document.getElementById("faceok").value = "'.$userface.'";</script>';
	    echo ('<div class="facebox">
    		<img src="'.$userface.'0.jpg" id="ImageDrag1" style="float:center;BORDER: #cccccc 1px solid; PADDING:6px">
                </div>
                <script language="javascript"> document.getElementById("ImageDrag1").src="'.$userface.'0.jpg"+"?temp="+ Date.parse(new Date()); </script>
                 '); 
            exit;
	}
	unset($img);
	break;
    }
require_once DIR_COMM.'template.func.php';
require_once tpl('cutpic.html', $tpldir);    
?>
